unit UMemoEdit;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
  Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.ComCtrls, Vcl.Buttons, Data.DB;

type
  TMemoEditForm = class(TForm)
    pnlBottom: TPanel;
    pnlMain: TPanel;
    lblTitle: TLabel;
    edtTitle: TEdit;
    lblContent: TLabel;
    memContent: TMemo;
    lblCategory: TLabel;
    cbbCategory: TComboBox;
    lblPriority: TLabel;
    cbbPriority: TComboBox;
    grpReminder: TGroupBox;
    chkReminder: TCheckBox;
    dtpRemindDate: TDateTimePicker;
    dtpRemindTime: TDateTimePicker;
    cbbRemindType: TComboBox;
    btnOK: TBitBtn;
    btnCancel: TBitBtn;
    
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure chkReminderClick(Sender: TObject);
    procedure btnOKClick(Sender: TObject);
  private
    FMemoID: Integer;
    procedure LoadCategories;
    procedure LoadMemoData;
    procedure EnableReminder(Enable: Boolean);
    function ValidateInput: Boolean;
  public
    class function EditMemo(AMemoID: Integer = 0): Boolean;
  end;

implementation

{$R *.dfm}

uses UDataModule;

{ TMemoEditForm }

class function TMemoEditForm.EditMemo(AMemoID: Integer = 0): Boolean;
var
  Form: TMemoEditForm;
begin
  Form := TMemoEditForm.Create(nil);
  try
    Form.FMemoID := AMemoID;
    Result := (Form.ShowModal = mrOk);
  finally
    Form.Free;
  end;
end;

procedure TMemoEditForm.FormCreate(Sender: TObject);
begin
  // 初始化优先级下拉框
  with cbbPriority.Items do
  begin
    Clear;
    Add('高');
    Add('中');
    Add('低');
  end;
  cbbPriority.ItemIndex := 1; // 默认中优先级

  // 初始化提醒类型下拉框
  with cbbRemindType.Items do
  begin
    Clear;
    Add('一次性');
    Add('每天');
    Add('每周');
    Add('每月');
  end;
  cbbRemindType.ItemIndex := 0;

  // 设置日期时间选择器格式
  dtpRemindDate.Date := Date;
  dtpRemindTime.Time := Time;
  dtpRemindTime.Format := 'HH:mm';

  EnableReminder(False);
  LoadCategories;
end;

procedure TMemoEditForm.LoadCategories;
begin
  with DM.qryCategory do
  begin
    Close;
    SQL.Text := 'SELECT ID, Name FROM Category ORDER BY Name';
    Open;
    
    cbbCategory.Items.Clear;
    while not EOF do
    begin
      cbbCategory.Items.AddObject(
        FieldByName('Name').AsString,
        TObject(FieldByName('ID').AsInteger)
      );
      Next;
    end;
    
    if cbbCategory.Items.Count > 0 then
      cbbCategory.ItemIndex := 0;
  end;
end;

procedure TMemoEditForm.FormShow(Sender: TObject);
begin
  if FMemoID > 0 then
    LoadMemoData
  else
  begin
    Caption := '新建备忘录';
    edtTitle.Clear;
    memContent.Clear;
  end;
end;

procedure TMemoEditForm.LoadMemoData;
begin
  Caption := '编辑备忘录';
  
  with DM.qryMemo do
  begin
    Close;
    SQL.Text := 'SELECT M.*, R.RemindTime, R.RemindType ' +
                'FROM Memo M ' +
                'LEFT JOIN Reminder R ON M.ID = R.MemoID ' +
                'WHERE M.ID = :ID';
    Parameters.ParamByName('ID').Value := FMemoID;
    Open;
    
    if not IsEmpty then
    begin
      edtTitle.Text := FieldByName('Title').AsString;
      memContent.Text := FieldByName('Content').AsString;
      
      // 设置分类
      for var i := 0 to cbbCategory.Items.Count - 1 do
        if Integer(cbbCategory.Items.Objects[i]) = FieldByName('CategoryID').AsInteger then
        begin
          cbbCategory.ItemIndex := i;
          Break;
        end;
      
      // 设置优先级
      cbbPriority.ItemIndex := FieldByName('Priority').AsInteger - 1;
      
      // 设置提醒
      if not FieldByName('RemindTime').IsNull then
      begin
        chkReminder.Checked := True;
        EnableReminder(True);
        dtpRemindDate.Date := FieldByName('RemindTime').AsDateTime;
        dtpRemindTime.Time := FieldByName('RemindTime').AsDateTime;
        cbbRemindType.ItemIndex := FieldByName('RemindType').AsInteger - 1;
      end;
    end;
  end;
end;

procedure TMemoEditForm.EnableReminder(Enable: Boolean);
begin
  dtpRemindDate.Enabled := Enable;
  dtpRemindTime.Enabled := Enable;
  cbbRemindType.Enabled := Enable;
end;

procedure TMemoEditForm.chkReminderClick(Sender: TObject);
begin
  EnableReminder(chkReminder.Checked);
end;

function TMemoEditForm.ValidateInput: Boolean;
begin
  Result := False;
  
  if Trim(edtTitle.Text) = '' then
  begin
    ShowMessage('请输入标题');
    edtTitle.SetFocus;
    Exit;
  end;
  
  if cbbCategory.ItemIndex < 0 then
  begin
    ShowMessage('请选择分类');
    cbbCategory.SetFocus;
    Exit;
  end;
  
  if chkReminder.Checked then
  begin
    if (dtpRemindDate.Date + dtpRemindTime.Time) < Now then
    begin
      ShowMessage('提醒时间不能早于当前时间');
      dtpRemindDate.SetFocus;
      Exit;
    end;
  end;
  
  Result := True;
end;

procedure TMemoEditForm.btnOKClick(Sender: TObject);
var
  MemoID: Integer;
begin
  if not ValidateInput then
    Exit;
    
  // 保存备忘录
  if FMemoID = 0 then
  begin
    MemoID := DM.AddMemo(
      edtTitle.Text,
      memContent.Text,
      Integer(cbbCategory.Items.Objects[cbbCategory.ItemIndex]),
      cbbPriority.ItemIndex + 1
    );
  end
  else
  begin
    MemoID := FMemoID;
    DM.UpdateMemo(
      FMemoID,
      edtTitle.Text,
      memContent.Text,
      Integer(cbbCategory.Items.Objects[cbbCategory.ItemIndex]),
      cbbPriority.ItemIndex + 1
    );
  end;
  
  // 保存提醒设置
  if chkReminder.Checked then
  begin
    DataModule.AddReminder(
      MemoID,
      dtpRemindDate.Date + dtpRemindTime.Time,
      cbbRemindType.ItemIndex + 1
    );
  end;
  
  ModalResult := mrOk;
end;

end. 